package com.dalianpai.gulimall.coupon.service.impl;

import com.dalianpai.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.dalianpai.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dalianpai.common.utils.PageUtils;
import com.dalianpai.common.utils.Query;

import com.dalianpai.gulimall.coupon.dao.SeckillSessionDao;
import com.dalianpai.gulimall.coupon.entity.SeckillSessionEntity;
import com.dalianpai.gulimall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> lates3DaySession() {
        //最近3天的秒杀活动列表
        List<SeckillSessionEntity> list = this.list(
                new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime(), endTime())
        );
        if(list!=null && list.size()>0){
                                                 //遍历最近3天秒杀活动列表，为每个秒杀活动设置他的商品列表，最后再次收集成一个新的秒杀活动列表
            List<SeckillSessionEntity> newScekillSessionList = list.stream().map(session -> {
                Long id = session.getId();       //查询某个秒杀活动对应的商品列表
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(
                        new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id",id)
                );
                System.out.println(relationEntities);
                session.setRelationSkus(relationEntities);//为每个秒杀活动设置他的商品列表
                return session;
            }).collect(Collectors.toList());//最后再次收集成一个新的秒杀活动列表(为每个秒杀活动设置了他的商品列表)，其实可以不用再收集新的集合应该也可以
            return newScekillSessionList;
        }
        return null;
    }

    //最近3天的开始时间     2021-02-19 00:00:00
    public String startTime(){
        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now, LocalTime.MIN);//今天的零时:零分:零秒      00:00:00
        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    //最近3天的结束时间     2021-02-21 23:59:59
    public String endTime(){
        LocalDate now = LocalDate.now();
        //最近3天就是在今天基础上面加2天
        LocalDate localDate = now.plusDays(2);
        LocalDateTime of = LocalDateTime.of(localDate, LocalTime.MAX);//2天后的23点59分59秒    23:59:59
        String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }



}